package br.com.android.math.fibonacci;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * 
 * @author mpatutti
 *
 */
public class ContactsFormatterActivity extends Activity implements OnClickListener {

    private Button   bGenerate;
    private Button   bClear;
    private ListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // getting the objects from the screen
        bGenerate = (Button) findViewById(R.id.bGenerate);
        bClear = (Button) findViewById(R.id.bClear);
        listView = (ListView) findViewById(R.id.listView1);

        /**
         * O metodo onClick da classe recebera o evento de click
         */
        bGenerate.setOnClickListener(this);

        bClear.setOnClickListener(new OnClickListener() {

            /**
             * Acao do botao bClear
             */
            @Override
            public void onClick(View v) {

                // clear all data
                // listView.setAdapter(null);
                // EditText e = (EditText) findViewById(R.id.editText1);
                // e.setText("");
            }
        });
    }

    /**
     * Acao do botao bGenerate
     */
    @Override
    public void onClick(View v) {

        // EditText e = (EditText) findViewById(R.id.editText1);
        // if (StringUtils.isEmpty(e.getText())) {
        // return;
        // }

        ContactUtils c = new ContactUtils(ContactsFormatterActivity.this);
        ArrayList<Relation> r = c.loadContacts();

        String[] from = new String[] { "rowid", "col" };// nome das keys do hashmap
        int[] to = new int[] { R.id.gridIdRow, R.id.gridText };// id dos elementos q receberao os valores da lista

        // list para mostrar na tela
        List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

        // enxe a lista, cada elemento eh um hashmap de 2 itens(uma linha)
        for (int i = 0; i < r.size(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("rowid", i + " - ");
            map.put("col", r.get(i).getFirstName() + " " + r.get(i).getLastName() );
            fillMaps.add(map);
        }

        // fill in the grid_item layout
        SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.itemlinhagrid, from, to);
        listView.setAdapter(adapter);
    }

}
